require("prefabutil")

local prefabs =
{
    "collapse_small",
}

-- 根据当前世界状态，决定灯光是开还是关
local function RecalcLightPhase(inst)
    if TheWorld:HasTag("cave") then
        inst.Light:Enable(true)
        inst.AnimState:Show("light")
        inst.AnimState:Show("shadow")
        inst.AnimState:Show("flames")
        inst.AnimState:PlayAnimation("idle", true)
        inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
    else
        local phase = TheWorld.state.phase
        if phase == "day" then
            inst.Light:Enable(false)
            inst.AnimState:Hide("light")
            inst.AnimState:Hide("shadow")
            inst.AnimState:Hide("flames")
            inst.AnimState:PlayAnimation("idle")
            inst.AnimState:ClearBloomEffectHandle()
        else
            inst.Light:Enable(true)
            inst.AnimState:Show("light")
            inst.AnimState:Show("shadow")
            inst.AnimState:Show("flames")
            inst.AnimState:PlayAnimation("idle", true)
            inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
        end
    end
end

local function onworkfinished(inst)
    inst.components.lootdropper:DropLoot()
    local fx = SpawnPrefab("collapse_small")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    fx:SetMaterial("wood")
    inst:Remove()
end

local function MakeLight()
    local assets =
    {
        Asset("ANIM", "anim/bboy_river_lantern.zip"),
    }

    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddMiniMapEntity()
        inst.entity:AddLight()
        inst.entity:AddNetwork()

        inst:SetDeploySmartRadius(0.5)

        -- MakeObstaclePhysics(inst, .4)
        MakeWaterObstaclePhysics(inst, 0.4, 0.5, 0.75)

        inst.MiniMapEntity:SetIcon("bboy_river_lantern.png")

        inst.AnimState:SetBank("bboy_river_lantern")
        inst.AnimState:SetBuild("bboy_river_lantern")
        inst.AnimState:PlayAnimation("idle")

        inst.Light:SetColour(1, 0.5, 0) -- 橙色光
        inst.Light:SetRadius(2.5)       -- 设置光照范围
        inst.Light:SetFalloff(0.85)     -- 设置光照衰减率
        inst.Light:SetIntensity(0.85)   -- 设置光照强度
        inst.Light:Enable(true)

        inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh") -- 加个泛光,游戏中可以在官方设置中开启关闭

        inst:AddTag("structure")
        inst:AddTag("lamp")
        inst:AddTag("decortable")

        MakeSnowCoveredPristine(inst)

        MakeInventoryFloatable(inst, "med", 0.2, { 0.95, 0.65, 1 })
        inst.components.floater:SetIsObstacle()

        local land_time = (POPULATING and math.random() * 5 * FRAMES) or 0
        inst:DoTaskInTime(land_time, function(inst)
            inst.components.floater:OnLandedServer()
        end)

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end

        MakeHauntableWork(inst)
        MakeSnowCovered(inst)

        inst:AddComponent("workable")
        inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
        inst.components.workable:SetWorkLeft(4)
        inst.components.workable:SetOnFinishCallback(onworkfinished)

        inst:AddComponent("inspectable")

        inst:AddComponent("lootdropper")

        inst:WatchWorldState("phase", RecalcLightPhase)
        RecalcLightPhase(inst)

        return inst
    end

    return Prefab("bboy_river_lantern", fn, assets, prefabs)
end

return MakeLight(),
    MakePlacer("bboy_river_lantern_placer", "bboy_river_lantern", "bboy_river_lantern", "idle", false, false, false, nil, nil, nil, nil, 2.8)
